//
//  SLFirmApproveViewController.m
//  StarLight
//
//  Created by apple on 16/8/23.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLFirmApproveViewController.h"
#import "SLNetWork.h"

@interface SLFirmApproveViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
/**营业执照*/
@property (nonatomic ,strong) UILabel *firmName;
/**说明*/
@property (nonatomic ,strong) UILabel *firmExplanation;
/**公司名称*/
@property (nonatomic ,strong) UILabel *firmCompanyName;
/**营业执照图片*/
@property (nonatomic ,strong) UIImageView *firmImg;
/**箭头*/
@property (nonatomic ,strong) UIImageView *firmArrowImg;
/**线条*/
@property (nonatomic ,strong) UIView *firmLine;
/**背后视图*/
@property (nonatomic ,strong) UIView *bgView;
/**提交*/
@property (nonatomic ,strong) UIButton *commitBtn;

@end

@implementation SLFirmApproveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addUI];
    [self addContaints];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = @"企业认证";
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)changeIcon{
    @weakify(self)
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"更换头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    sheet.title = nil;
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
        picker.delegate = self;
        picker.allowsEditing = YES;//设置可编辑
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];//进入照相界面
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"本地获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self)
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
        picker.delegate = self;
        picker.allowsEditing = YES;//设置可编辑
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:picker animated:YES completion:nil];//进入照相界面
    }];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [sheet addAction:action1];
    [sheet addAction:action2];
    [sheet addAction:action3];
    [self presentViewController:sheet animated:YES completion:nil];
}

#pragma mark - 照相机代理方法
//点击相册中的图片 货照相机照完后点击use  后触发的方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    @weakify(self);
    [picker dismissViewControllerAnimated:YES completion:^{
        @strongify(self)
        // 上传照片
        UIImage *icon = info[UIImagePickerControllerOriginalImage];
        self.firmImg.image = icon;
    }];
}
//提交审核
- (void)saveImg{
    NSData *imgData = UIImageJPEGRepresentation(self.firmImg.image, 0.3);
    [SLNetWork upLoadImage:imgData sender:@"image" sendeName:@"companyname" complete:^(id respond, NSError *error) {
        if (!error) {
            [SVProgressHUD showSuccessWithStatus:respond[@"result"]];
        }
    }];
}


- (void)addUI{
    self.view.backgroundColor = [UIColor defeutBackgroudColor];
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.firmName];
    [self.bgView addSubview:self.firmImg];
    [self.bgView addSubview:self.firmLine];
    [self.bgView addSubview:self.firmArrowImg];
    [self.bgView addSubview:self.firmCompanyName];
    [self.bgView addSubview:self.firmExplanation];
    [self.view addSubview:self.commitBtn];
}
- (void)addContaints{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(74);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(160 * MIX_H);
    }];
    [self.firmName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).with.offset(14);
        make.left.equalTo(self.view).with.offset(16);
    }];
    [self.firmImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView).with.offset(10);
        make.right.equalTo(self.view).with.offset(-16);
        make.size.mas_equalTo(CGSizeMake(102, 75));
    }];
    
    [self.firmExplanation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.firmImg.mas_left).with.offset(-26);
        make.top.equalTo(self.firmName.mas_bottom).with.offset(31);
        make.left.equalTo(self.firmName);
    }];
    
    [self.firmLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.bgView);
        make.height.mas_equalTo(1);
        make.top.equalTo(self.firmExplanation.mas_bottom).with.offset(11);
    }];
    [self.firmCompanyName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.firmLine.mas_bottom).with.offset(27);
        make.left.equalTo(self.firmName);
    }];
    [self.firmArrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.firmImg);
        make.centerY.equalTo(self.firmCompanyName);
        make.height.mas_equalTo(14);
    }];
    
    [self.commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(27);
        make.right.equalTo(self.view).with.offset(-27);
        make.height.mas_equalTo(46);
        make.bottom.equalTo(self.view).with.offset(-66);
    }];
}
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UILabel *)firmName{
    if (!_firmName) {
        _firmName = [UILabel new];
        _firmName.text = @"营业执照";
        _firmName.font = sFont(14);
        _firmName.textColor = RGBCOLOR(51, 51, 51);
    }
    return _firmName;
}
- (UILabel *)firmExplanation{
    if (!_firmExplanation) {
        _firmExplanation = [UILabel new];
        _firmExplanation.font = sFont(10);
        _firmExplanation.text = @"说明：支持JPG、PNG、BMP图片文件，大小不超过5M";
        _firmExplanation.numberOfLines = 2;
        _firmExplanation.textColor = RGBCOLOR(153, 153, 153);
    }
    return _firmExplanation;
}
- (UIImageView *)firmImg{
    if (!_firmImg) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeIcon)];
        _firmImg = [UIImageView new];
        _firmImg.userInteractionEnabled = YES;
        _firmImg.image = IMG(@"head");
        [_firmImg addGestureRecognizer:tap];
    }
    return _firmImg;
}
- (UIView *)firmLine{
    if (!_firmLine) {
        _firmLine = [UIView new];
        _firmLine.backgroundColor = RGBCOLOR(223, 223, 223);
    }
    return _firmLine;
}
- (UILabel *)firmCompanyName{
    if (!_firmCompanyName) {
        _firmCompanyName = [UILabel new];
        _firmCompanyName.font = sFont(14);
        _firmCompanyName.text = @"公司名称";
        _firmCompanyName.textColor = RGBCOLOR(51, 51, 51);
    }return _firmCompanyName;
}
- (UIImageView *)firmArrowImg{
    if (!_firmArrowImg) {
        _firmArrowImg = [UIImageView new];
        _firmArrowImg.image = IMG(@"ic_arrow_gray");
    }
    return _firmArrowImg;
}
- (UIButton *)commitBtn{
    if (!_commitBtn) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitBtn.backgroundColor = RGBCOLOR(254,159,0);
        [_commitBtn setTitle:@"提交" forState:(UIControlStateNormal)];
        [_commitBtn setTitleColor:RGBCOLOR(255, 255, 255) forState:(UIControlStateNormal)];
        _commitBtn.titleLabel.font = sFont(20);
        [_commitBtn addTarget:self action:@selector(saveImg) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _commitBtn;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
